package com.husk.leetcode.competition;

import org.junit.Test;

import java.util.Arrays;

/**
 * <p>
 *
 * </p>
 *
 * @author hushunke@foxmail.com
 * @since 2021/8/1
 */
public class Solution {

    public boolean isThree(int n) {
        if (n <= 3) {
            return false;
        }
        int count = 0;
        for (int i = 1; i <= n; i++) {
            int temp = n / i;
            if (temp * i == n) {
                count++;
            }
            if (count > 3) {
                return false;
            }
        }
        return count == 3;
    }

    public long numberOfWeeks(int[] milestones) {
        long longest = Arrays.stream(milestones).mapToLong(Long::valueOf).max().getAsLong();
        long rest = Arrays.stream(milestones).mapToLong(Long::valueOf).sum() - longest;
        if (longest > rest + 1) {
            return rest * 2 + 1;
        } else {
            return longest + rest;
        }
    }

    /**
     * 5187. 收集足够苹果的最小花园周长
     * https://leetcode-cn.com/problems/minimum-garden-perimeter-to-collect-enough-apples/
     *
     * @param neededApples
     * @return
     */
    public long minimumPerimeter(long neededApples) {
        // s = 2n(n+1)(2n+1)
        long n = 1;
        while (2 * n * (n + 1) * (2 * n + 1) < neededApples) {
            n++;
        }
        return 8 * n;
    }

    /**
     * 5193. 删除字符使字符串变好
     * https://leetcode-cn.com/problems/delete-characters-to-make-fancy-string/
     *
     * @param s
     * @return
     */
    public String makeFancyString(String s) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < s.length(); i++) {
            int n = sb.length();
            char c = s.charAt(i);
            if (n >= 2 && sb.charAt(n - 1) == c && sb.charAt(n - 2) == c) {
                continue;
            } else {
                sb.append(c);
            }
        }
        return sb.toString();
    }

    /**
     * 1958. 检查操作是否合法
     * https://leetcode-cn.com/problems/check-if-move-is-legal/
     * <p>
     * board.length == board[r].length == 8
     * 0 <= rMove, cMove < 8
     * board[rMove][cMove] == '.'
     * color要么是'B' 要么是'W'。
     *
     * @param board
     * @param rMove
     * @param cMove
     * @param color
     * @return
     */
    public boolean checkMove(char[][] board, int rMove, int cMove, char color) {
        // 以rMove和cMove的8个方向，3个或3更多格子
        int[][] dirs = new int[][]{{0, -1}, {0, 1}, {-1, 0}, {1, 0}, {-1, -1}, {1, 1}, {-1, 1}, {1, -1}};
        for (int[] dir : dirs) {
            int x = dir[0], y = dir[1];
            int r = rMove + x, c = cMove + y, cnt = 0;
            while (r >= 0 && r < 8 && c >= 0 && c < 8) {
                if (cnt >= 1 && board[r][c] == color) {
                    return true;
                }
                if (board[r][c] == '.') {
                    break;
                }
                if (cnt == 0 && board[r][c] == color) {
                    break;
                }
                cnt++;
                r += x;
                c += y;
            }
        }
        return false;
    }


    @Test
    public void test() {
        int[] req = new int[]{1, 2, 3};
        long ans = this.numberOfWeeks(req);
        System.out.println(ans);
    }

}
